package com.zhangsun.system.controller;


import com.zhangsun.base.bean.ResponseData;
import com.zhangsun.common.utils.ResponseUtil;
import com.zhangsun.system.entity.DictTypeEntity;
import com.zhangsun.system.service.DictTypeService;
import com.zhangsun.system.vo.dict.DictTypeEditVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 数据字典类型 前端控制器
 * </p>
 *
 * @author zhangsun
 * @since 2019-08-13
 */
@RestController
@RequestMapping("/dictType")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 获取所有分类
     *
     * @return 分类
     */
    @GetMapping("/findAll")
    public ResponseData findAll() {
        List<DictTypeEntity> dictTypes = dictTypeService.findAll();
        return ResponseUtil.success(dictTypes);
    }

    /**
     * 添加字典分类
     *
     * @param vo 分类
     * @return 结果
     */
    @PostMapping("/create")
    @RequiresPermissions("system:dict:edit")
    public ResponseData create(@Validated DictTypeEditVo vo) {
        dictTypeService.create(vo);
        return ResponseUtil.success();
    }

    /**
     * 字典分类详情
     *
     * @param id 分类id
     * @return 详情
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:dict:view")
    public ResponseData findById(@NotBlank @PathVariable String id) {
        DictTypeEntity dictTypeEntity = dictTypeService.findById(id);
        return ResponseUtil.success(dictTypeEntity);
    }

    /**
     * 更新字典分类
     *
     * @param id 分类id
     * @param vo 分类
     * @return 结果
     */
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:dict:edit")
    public ResponseData update(@NotBlank @PathVariable String id, @Validated DictTypeEditVo vo) {
        dictTypeService.update(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 删除字典分类
     *
     * @param id 分类id
     * @return 结果
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:dict:delete")
    public ResponseData delete(@NotBlank @PathVariable String id) {
        dictTypeService.delete(id);
        return ResponseUtil.success();
    }

}
